<template>
<!-- 任务 -->
  <el-cascader
      :options="options"
      :filterable="true"
      :props="{ checkStrictly: true }"
      @change="change"
      class="task_cascader"
      clearable></el-cascader>
</template>

<script>
import vSelectTablePage from '@/components/element/vSelectT/vSelectTablePage'
import projectTaskApi from "@/api/projectTaskApi";
export default {
  name: "SelectDepart",
  components: {
    vSelectTablePage
  },
  props: {
    value: {
      type: [String, Number, Array],
      default: function () {
        return undefined
      }
    },
    column: {
      type: [Array, Object, String],
      default: function () {
        return [
          {
            field: 'Name',
          }
        ]
      }
    },
    multiple : {
      type: Boolean,
      default: function () {
        return false
      }
    },
    showHeader: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    clickValue: {
      type: [String, Array],
      default: function () {
        return 'Id'
      }
    },
    queryValue: {
      type: Object,
      default: function () {
        return {}
      }
    },
    url: {
      type: Object,
      default: function () {
        return undefined
      }
    }
  },
  data () {
    return {
      load: false,
      options: [],
      projectTaskApi: projectTaskApi
    }
  },
  methods: {
    change (a) {
      let selectList = this.options.filter(it=> a.some(it1 => it1 === it.value))
      console.log(selectList)
      this.$emit('change', a, selectList)
    },
    selectChange (value) {
      this.$emit('selectChanged', value)
    },
    initLoad () {
      this.projectTaskApi.getPage(this.queryValue).then(res => {
        let ww = res.filter(it => {
          return it.Classify !== '3'
        })
        let Parenttaskids = (ww||[]).filter(it => it.Parenttaskid === '0').map(it => {
          return {
            value: it.Id,
            label: it.Name,
            Planid: it.Planid
          }
        })
        if (Parenttaskids.length > 0) {
          this.hanldChild((ww||[]), Parenttaskids)
        }
        this.options = Parenttaskids
      })
    },
    hanldChild (data, Parenttaskids) {
      Parenttaskids.forEach(it => {
        let t = data.filter(it1 => it1.Parenttaskid === it.value)
        if (t.length > 0) {
          if (!it.hasOwnProperty('children')) {
            this.$set(it, 'children', [])
          }
          t.forEach(it1 => {
            it.children.push({
              value: it1.Id,
              label: it1.Name
            })
          })
          this.hanldChild(data, it.children || [])
        }
      })
    }
  },
  watch: {
    load () {
      this.initLoad()
    }
  }
}
</script>

<style scoped>
.task_cascader {
  width: 100%;
}
</style>
